﻿package com.tongji.housediy.view
{
	import com.tongji.housediy.events.TypeXMLAdminEvent;
	import com.tongji.housediy.events.UserLoadandRegisterEvent.LoginEvent;
	import com.tongji.housediy.events.commonEvent.ResetRadioGroupEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideControlPanelEvent;
	import com.tongji.housediy.events.commonEvent.ShowHideTitleWindowEvent;
	import com.tongji.housediy.events.goodsEvent.DataDoneEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandNameEvent;
	import com.tongji.housediy.events.goodsEvent.GetBrandPicSrcEvent;
	import com.tongji.housediy.events.goodsEvent.GetGoodsByDictEvent;
	import com.tongji.housediy.events.goodsEvent.SearchGoodsEvent;
	import com.tongji.housediy.model.BrandModel;
	import com.tongji.housediy.model.CompEnableStateModel;
	import com.tongji.housediy.model.DesignModel;
	import com.tongji.housediy.model.ExchangeDataModel;
	import com.tongji.housediy.model.HouseModel;
	import com.tongji.housediy.model.RightCompStateModel;
	import com.tongji.housediy.model.RightListStateModel;
	import com.tongji.housediy.model.SearchResultModel;
	import com.tongji.housediy.model.UserInforModel;
	import com.tongji.housediy.service.UserService;
	import com.tongji.housediy.utils.TextFieldPool;
	import com.tongji.housediy.view.titlewindows.LoginTitleWindow;
	import com.tongji.housediy.view.titlewindows.RegisterTitleWindow;
	
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.external.ExternalInterface;
	import flash.net.SharedObject;
	
	import mx.binding.utils.BindingUtils;
	import mx.controls.Alert;
	import mx.core.FlexGlobals;
	import mx.events.FlexEvent;
	import mx.managers.PopUpManager;
	
	import org.robotlegs.mvcs.Mediator;
	
	import spark.components.TitleWindow;
	import spark.events.IndexChangeEvent;
	import spark.events.TextOperationEvent;
	
	/**
	 * 
	 * @author chenchao
	 * 
	 */
	public class RightControlCompMediator extends Mediator
	{
		[Inject]
		public var view:RightControlComp;
		
		[Inject]
		public var model:RightCompStateModel;
		
		[Inject]
		public var stateModel:RightListStateModel;
		
		[Inject]
		public var enableModel:CompEnableStateModel;
		
		[Inject]
		public var brandNameModel:BrandModel;
		
		[Inject]
		public var searchGoodsBrandModel:SearchResultModel;
		
		[Inject]
		public var designModel:DesignModel;
		
		[Inject]
		public var userModel:UserInforModel;
		

		
		[Inject]
		public var service:UserService;
		
		private var currentState:String = "State1";
		
		override public function onRegister():void
		{		
			//搜索下拉框
			BindingUtils.bindProperty(view.relatedList, "dataProvider", brandNameModel, "brandNameArrayCollection");
			
			//view.textInput.addEventListener(TextOperationEvent.CHANGE, textChangeHandler);
			view.textInput.addEventListener(Event.ADDED, textChangeHandler);
			view.relatedList.addEventListener(IndexChangeEvent.CHANGE, listItemSelected);
			
			//搜索按钮
			view.searchButton.addEventListener(MouseEvent.MOUSE_DOWN, searchHandler);			
			view.textInput.addEventListener(FlexEvent.ENTER, searchHandler);

			view.hiddenBtn.addEventListener(MouseEvent.CLICK, hiddenBtnClickHandler);
			
			//radioButtonchange
			view.brandsName.addEventListener(MouseEvent.CLICK, radioButtonChangeHandler);
			view.goodsName.addEventListener(MouseEvent.CLICK, radioButtonChangeHandler);
			
			//注册登录
			view.registerBtn.addEventListener(MouseEvent.CLICK, registerHandler);
			view.loginBtn.addEventListener(MouseEvent.CLICK, loginHandler);
			this.addContextListener(LoginEvent.UPDATE_USER, loginSuccessHandler, LoginEvent);
			this.addContextListener(SearchGoodsEvent.UPDATE_SEARCH_RESULT, searchResultHandler, SearchGoodsEvent);
			//BindingUtils.bindProperty(view.userInfor, "text", userModel, "userID");
		

		}
						
		private function showSearchResultHandler(event:MouseEvent):void
		{
			view.firstLine.currentState = "search";
		}
		
		private function hiddenBtnClickHandler(event:MouseEvent):void
		{
			var state:String = FlexGlobals.topLevelApplication.currentState;
			if (state == 'initial')
			{
				dispatch(new ShowHideControlPanelEvent(ShowHideControlPanelEvent.HIDE_CONTROL_PANEL));
				FlexGlobals.topLevelApplication.currentState = 'hide';
			}
			else
			{
				dispatch(new ShowHideControlPanelEvent(ShowHideControlPanelEvent.SHOW_CONTROL_PANEL));
				FlexGlobals.topLevelApplication.currentState = 'initial';
			}
			
			// 隐藏 TitleWindow
			dispatch(new ShowHideTitleWindowEvent(ShowHideTitleWindowEvent.HIDE_TITLE_WINDOW));
		}
			
		private function textChangeHandler(event:Event):void
		{
			if (view.brandsName.selected)
			{
				if (view.textInput.text.length > 0)
				{
					dispatch(new GetBrandNameEvent(GetBrandNameEvent.GET_BRAND_NAME, view.textInput.text));
					eventMap.mapListener(eventDispatcher, GetBrandNameEvent.UPDATE_BRAND_MODEL, getBrandNameHandler);
				}
				else if (view.textInput.text.length == 0)
				{
					brandNameModel.brandNameArrayCollection.removeAll();
					view.relatedList.visible = false;
				}
			}
		}
		
		private function getBrandNameHandler(event:Event):void
		{
			if (brandNameModel.brandNameArrayCollection.length > 0)
			{
				view.relatedList.visible = true;
			}
			if (brandNameModel.brandNameArrayCollection.length == 0)
			{
				view.relatedList.visible = false;
			}
			eventMap.unmapListener(eventDispatcher, GetBrandNameEvent.UPDATE_BRAND_MODEL, getBrandNameHandler);
		}
		
		private function searchHandler(event:Event):void
		{			
			view.relatedList.visible = false;
			view.firstLine.currentState = "search";
			view.firstLine.firstLayorShowComp.currentState = "State4";
			view.firstLine.searchResultsTabBtn.selected = true;
			if(view.textInput.text.length > 0)
			{
				if(view.goodsName.selected == true)
					dispatch(new SearchGoodsEvent(SearchGoodsEvent.SEARCH_GOODS, false, view.textInput.text));
				else
					dispatch(new SearchGoodsEvent(SearchGoodsEvent.SEARCH_GOODS, true, view.textInput.text));				
				view.firstLine.backToMainTabBtn.addEventListener(MouseEvent.CLICK, backToMainHandler);
			}
			else 
			{
				view.currentState = 'State9';			
			}
		}
		
		//"返回主功能"按钮事件
		private function backToMainHandler(event:MouseEvent):void
		{		
			view.firstLine.currentState = "normal";
			view.firstLine.firstLayorShowComp.currentState = "State1";
			view.textInput.text = "";
		}
		
		private function listItemSelected(event:IndexChangeEvent):void
		{
			view.textInput.text = view.relatedList.selectedItem;
			view.relatedList.visible = false;
		}
		
		private function radioButtonChangeHandler(event:MouseEvent):void
		{
			view.textInput.text = "";
			view.relatedList.visible = false;
		}
		
		private function getBrandPicHandler(event:Event):void
		{
			if (searchGoodsBrandModel.brandPicSrc.length > 0)
			{
				view.firstLine.firstLayorShowComp.currentState = 'State4';
				enableModel.isSearchState = true;
				view.firstLine.currentState = "search";
				view.firstLine.searchResultsTabBtn.addEventListener(MouseEvent.CLICK, showSearchResultHandler);

				view.firstLine.searchResultsTabBtn.selected = true;
				//view.firstLine.firstLayorShowComp.brandLogo.source = searchGoodsBrandModel.brandPicSrc;
			}
			eventMap.unmapListener(eventDispatcher, GetBrandPicSrcEvent.UPDATE_BRAND_PIC_SRC, getBrandPicHandler);
		}
		
		private function searchResultHandler(event:Event):void
		{
			if (searchGoodsBrandModel.searchResultArray.length > 0)
			{
				view.firstLine.firstLayorShowComp.currentState = 'State5';
				//view.firstLine.firstLayorShowComp.goodsTotal.text = "共" + total.toString() + "个商品";
				view.firstLine.firstLayorShowComp.searchResult.bigList.dataGroup.dataProvider = searchGoodsBrandModel.searchResultArray;
			}
			else if(searchGoodsBrandModel.searchResultArray.length == 0)
			{
				view.firstLine.firstLayorShowComp.currentState = 'State6';
				view.firstLine.currentState = "search";
				view.firstLine.searchResultsTabBtn.addEventListener(MouseEvent.CLICK, showSearchResultHandler);

				view.firstLine.searchResultsTabBtn.selected = true;
			}
	
			
			//eventMap.unmapListener(eventDispatcher, SearchGoodsEvent.UPDATE_GOODS_BRAND_MODEL, searchBrandViewHandler);
			view.firstLine.backToMainTabBtn.addEventListener(MouseEvent.CLICK, backToMainHandler);
		}
		
		private function  stateChangeHandler(event:Event):void
		{
			if (view.firstLine.furnitureTabBtn.selected == true)
			{
				view.firstLine.firstLayorShowComp.currentState = "State2";
				view.firstLine.currentState = "normal";
			}	
		}
		
		//注册事件,弹出注册框
		private function registerHandler(event:MouseEvent):void
		{
			var creatNewAccountWindow:RegisterTitleWindow = new RegisterTitleWindow();
			PopUpManager.addPopUp(creatNewAccountWindow, view.parent, true);
			PopUpManager.centerPopUp(creatNewAccountWindow);
			mediatorMap.createMediator(creatNewAccountWindow);
		}
		
		//登录事件,弹出登录框
		private function loginHandler(event:MouseEvent):void
		{
			var enterWindow:LoginTitleWindow = new LoginTitleWindow();
			enterWindow.name = "login";
			PopUpManager.addPopUp(enterWindow, view.parent, true);
			PopUpManager.centerPopUp(enterWindow);
			mediatorMap.createMediator(enterWindow);									
		}
		
		private function loginSuccessHandler(event:LoginEvent):void
		{
			view.loginBtn.label = "退出";
			view.registerBtn.label = "您好，" + userModel.userName;
			view.registerBtn.enabled = false;
			view.loginBtn.removeEventListener(MouseEvent.CLICK, loginHandler);
			view.loginBtn.addEventListener(MouseEvent.CLICK, logoutHandler);
		}
		
		private function logoutHandler(event:MouseEvent):void
		{
			userModel.isLogined = false;
			service.ws.logout();
		    userModel.userID = "";
			view.loginBtn.label = "登录";
			view.registerBtn.label = "注册";
			view.registerBtn.enabled = true;
			view.loginBtn.removeEventListener(MouseEvent.CLICK, logoutHandler);
			view.loginBtn.addEventListener(MouseEvent.CLICK, loginHandler);
		}
	} 
}